package com.tcm.controller.front;

import com.tcm.common.enums.ResponseEnum;
import com.tcm.entity.resp.R;
import com.tcm.service.NotificationService;
import com.tcm.utils.AccessTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 学生通知控制器
 * @author Guqier
 * @version 1.0
 * @date 2023/8/23 16:25
 */
@RestController
@RequestMapping("/stu/notification")
public class StuNotificationController {

    @Autowired
    private NotificationService notificationService;

    /**
     * 学生获取通知消息列表
     * @param current 当前页号
     * @param pageSize 页面大小
     * @param request 请求对象
     * @return 通知列表
     */
    @RequestMapping(value = "/normal", method = RequestMethod.GET)
    public R getStuNotifications(@RequestParam Integer current, @RequestParam Integer pageSize, HttpServletRequest request){
        return R.success(notificationService.getStuNotifications(current, pageSize, AccessTokenUtil.getUserId(request)));
    }

    /**
     * 学生获取最新滚动通知
     * @param request 请求对象
     * @return 最新滚动通知
     */
    @RequestMapping(value = "/roll", method = RequestMethod.GET)
    public R getStuRollNotifications(HttpServletRequest request){
        return R.success(notificationService.getStuRollNotification(AccessTokenUtil.getUserId(request)));
    }

    /**
     * 已读通知
     * @param id 通知ID
     * @param request 请求对象
     * @return 处理结果
     */
    @RequestMapping(method = RequestMethod.PUT)
    public R readNotification(@RequestParam Long id, HttpServletRequest request){
        return notificationService.readNotification(id, AccessTokenUtil.getUserId(request)) ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

}
